//
//  WCNFansDetailVC.m
//  WCN
//
//  Created by wsj2015 on 15/9/24.
//  Copyright (c) 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNFansDetailVC.h"
#import "WCNFansDetail.h"
#import "WCImageView +setImage.h"
#import "Macros.h"
#import "WCNetworkAccessManager.h"
#import "WCPopupLayerManager.h"
#import "WCAddressCell.h"


@interface WCNFansDetailVC ()<UITableViewDelegate,UITableViewDataSource>

{
    NSMutableArray *_dataArray;
    
    WCNFansDetail *_fansDetail;
    

}
@property (weak, nonatomic) IBOutlet UIImageView *fansIconImageView;
@property (weak, nonatomic) IBOutlet UILabel *fansPhoneLabel;
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;


@end

@implementation WCNFansDetailVC
- (instancetype)init
{
    if (self =[super init]) {
        _dataArray =[NSMutableArray array];
        self.title =@"关注我的用户";
        
    }
    return self;

}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor =kAppBg;
    self.bgView.layer.borderWidth =1;
    self.bgView.layer.borderColor =kBorderColor.CGColor;
    self.fansIconImageView.layer.cornerRadius =40;
    self.fansIconImageView.layer.masksToBounds = YES;
    [self.tableView registerNib:[UINib nibWithNibName:@"WCAddressCell" bundle:nil] forCellReuseIdentifier:addressCellId];
    [self getAddressList];
    
    
}

- (void)getAddressList
{
    
    MBProgressHUD *hud = showHud;
    weakSelf_define;
    [[WCNetworkAccessManager sharedMg] POST:kAddressListUrl parameters:@{@"patientId":@(self.patienId)} success:^(NSDictionary * responseObject) {
        NSDictionary *dataDict =responseObject[@"data"];
        WCNFansDetail *fans =[[WCNFansDetail alloc]init];
        
        [fans setValuesForKeysWithDictionary:dataDict];
        fans.addressArray =[weakSelf handleWithDataArray:dataDict[@"addresses"] className:@"WCNFasAddress"];
        _fansDetail =fans;
       
        [weakSelf reFreshUI];
    } failure:nil netError:nil always:^{
        [hud hide:YES];
        
    }];
    
 

}
- (NSArray *)handleWithDataArray:(NSArray *)dataArray className:(NSString *)className
{
    NSMutableArray *array =[NSMutableArray array];
    for (NSDictionary *dict in dataArray) {
        Class cls =NSClassFromString(className);
        id model  = [[cls alloc]init];
        [model setValuesForKeysWithDictionary:dict];
        [array addObject:model];
    }
    return array;

}



- (void)reFreshUI
{
    if (!_fansDetail) {
        return;
    }
    self.fansPhoneLabel.text =_fansDetail.mobile;
    [self.fansIconImageView setImageWithUrl:_fansDetail.avatarFileId];
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return _fansDetail.addressArray.count;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WCAddressCell *cell =[tableView dequeueReusableCellWithIdentifier:addressCellId forIndexPath:indexPath];
    WCNFasAddress *address =_fansDetail.addressArray[indexPath.row];
    [cell configModel:address];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(5, 10, kScreenWidth-10, 30)];
    label.text =@"常用服务地址";
    label.backgroundColor =kAppBg;
    label.font =[UIFont systemFontOfSize:16];
    return label;

}
@end
